package imagem;

import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core;
import com.googlecode.javacv.cpp.opencv_core.CvBox2D;
import com.googlecode.javacv.cpp.opencv_core.CvContour;
import com.googlecode.javacv.cpp.opencv_core.CvPoint2D32f;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;
import com.googlecode.javacv.cpp.opencv_core.CvSize2D32f;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_core.cvAbsDiff;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import org.jfree.ui.RefineryUtilities;

public class Imagem {
    
    public static void main(String[] args)  throws Exception{
        executarImagem();
        //executarWebcam();
    }
    
    public static void executarImagem(){
        Img i = new Img();
        
        //int[][][]m01 = i.arquivoToMatriz_RGB("files/people.jpg");
        //int[][][]m02 = i.pele1(m01);
        //i.matrizToArquivo_RGB(m02, "files/pele5");
        int[][] m01 = i.arquivoToMatriz("files/equalization.jpg");
        //int[][] m02 = i.histograma_equalizado(i.histograma(m01));
        int[][] m02 = i.equalizar(m01);
        
        i.matrizToArquivo(m01, "files/original.jpg");
        i.matrizToArquivo(m02, "files/equalizado.jpg");
        
        //i.binariza(matriz02,150);
        //i.objectCounter(matriz02);

        //Instancia e exibe formulario com duas imagens
        Formulario f = new Formulario();
        f.addImage(i.matrizToImagem(m01));
        f.addImage(i.matrizToImagem(m02));
        f.exibir();
        
        Chart demo = new Chart("Histograma Original",m01);
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
        
        Chart demo2 = new Chart("Histograma Equalizado",m02);
        demo2.pack();
        RefineryUtilities.centerFrameOnScreen(demo2);
        demo2.setVisible(true);
    }
    
    public static void executarWebcam() throws Exception{
        Formulario f = new Formulario();
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();

        opencv_core.IplImage frame = grabber.grab();
        opencv_core.IplImage image = null;
        opencv_core.IplImage prevImage = null;
        opencv_core.IplImage diff = null;
        
        CanvasFrame canvasFrame = new CanvasFrame("Webcam");
        canvasFrame.setCanvasSize(frame.width(), frame.height());
        
        CanvasFrame canvasImage = new CanvasFrame("Webcam");
        canvasImage.setCanvasSize(frame.width(), frame.height());
        
        /*CanvasFrame canvasPrev = new CanvasFrame("Webcam");
        canvasPrev.setCanvasSize(frame.width(), frame.height());
        
        CanvasFrame canvasDiff = new CanvasFrame("Webcam");
        canvasDiff.setCanvasSize(frame.width(), frame.height());*/
        

        opencv_core.CvMemStorage storage = opencv_core.CvMemStorage.create();

        while (canvasFrame.isVisible() && (frame = grabber.grab()) != null) {
            cvSmooth(frame, frame, CV_GAUSSIAN, 9, 9, 2, 2);
            //cvSobel(frame,frame,1,0,3);
            if (image == null) {
                image = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 1);
                cvCvtColor(frame, image, CV_RGB2GRAY);
            } else {
                prevImage = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 1);
                prevImage = image;
                image = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 1);
                cvCvtColor(frame, image, CV_RGB2GRAY);
            }

            if (diff == null) {
                diff = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U, 1);
            }

            if (prevImage != null) {
                // perform ABS difference
                cvAbsDiff(image, prevImage, diff);
                // do some threshold for wipe away useless details
                cvThreshold(diff, diff, 64, 255, CV_THRESH_BINARY);

                canvasFrame.showImage(frame);
                canvasImage.showImage(diff);
                
                // recognize contours
                CvSeq contour = new CvSeq(null);
                cvFindContours(diff, storage, contour, Loader.sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

                while (contour != null && !contour.isNull()) {
                    if (contour.elem_size() > 0) {
                        CvBox2D box = cvMinAreaRect2(contour, storage);
                        // test intersection
                        if (box != null) {
                            CvPoint2D32f center = box.center();
                            CvSize2D32f size = box.size();
/*                            for (int i = 0; i < sa.length; i++) {
                                if ((Math.abs(center.x - (sa[i].offsetX + sa[i].width / 2))) < ((size.width / 2) + (sa[i].width / 2)) &&
                                    (Math.abs(center.y - (sa[i].offsetY + sa[i].height / 2))) < ((size.height / 2) + (sa[i].height / 2))) {

                                    if (!alarmedZones.containsKey(i)) {
                                        alarmedZones.put(i, true);
                                        activeAlarms.put(i, 1);
                                    } else {
                                        activeAlarms.remove(i);
                                        activeAlarms.put(i, 1);
                                    }
                                    System.out.println("Motion Detected in the area no: " + i +
                                            " Located at points: (" + sa[i].x + ", " + sa[i].y+ ") -"
                                            + " (" + (sa[i].x +sa[i].width) + ", "
                                            + (sa[i].y+sa[i].height) + ")");
                                }
                            }
*/
                        }
                    }
                    contour = contour.h_next();
                }
            }
        }
        grabber.stop();
        canvasFrame.dispose();
    }

}
